package com.sun.officeoa.servlet;

import com.sun.officeoa.dao.CountDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyServletContextListener implements ServletContextListener {

    @Autowired
    private CountDao countDao;

    public static final Logger logger = LoggerFactory.getLogger(MyHttpSessionListener.class);
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        /**
         * @Description //TODO 登陆监听当前是第多少位访客
         * @Date 14:21 2019/3/8
         * @Param [servletContextEvent]
         * @return void
         **/
        int count = countDao.getCount();
        servletContextEvent.getServletContext().setAttribute("vc", count);
        logger.info("ServletContex初始化");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

        /**
         * @Description //TODO 浏览器关闭得时候，更新数据库数据
         * @Date 14:21 2019/3/8
         * @Param [servletContextEvent]
         * @return void
         **/
        Object visitcount = servletContextEvent.getServletContext().getAttribute("vc");
        countDao.updateCount(Integer.parseInt(visitcount.toString()));
        logger.info("ServletContex销毁");
    }
}
